package online.hxstart.code.concurrent.chapter07;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author huxing
 * @date 19-5-31 星期五 下午6:22
 * @describe
 */
public class ChapterThreadException {

  public static void main(String[] args) {

    //
    Thread.setDefaultUncaughtExceptionHandler(
        ((t, e) -> {
          System.out.println(t.getName() + " occur exception");
          e.printStackTrace();
        }));
    final Thread thread =
        new Thread(
            () -> {
              try {
                TimeUnit.SECONDS.sleep(2);
              } catch (InterruptedException e) {
              }
              System.out.println(1 / 0);
            },
            "Test-Thread");
    AtomicReference<String> aaaa = new AtomicReference<>();
    thread.setUncaughtExceptionHandler(
        ((t, e) -> {
          System.out.println(t.getName());
          e.printStackTrace();
          aaaa.set(t.getName());
        }));
    thread.start();
    System.out.println(aaaa);
  }
}
